package cn.com.haopy.yue.ai.wx.web;


import cn.com.haopy.yue.ai.core.utils.ResponseUtil;
import cn.com.haopy.yue.ai.db.entity.arisk.AIRiskStandardCalculate;
import cn.com.haopy.yue.ai.db.entity.arisk.AIRiskStandardCategory;
import cn.com.haopy.yue.ai.db.entity.arisk.AIRiskStandardCharges;
import cn.com.haopy.yue.ai.db.entity.arisk.AIRiskStandardChargesCollect;
import cn.com.haopy.yue.ai.db.service.hos.*;
import cn.com.haopy.yue.ai.wx.annotation.LoginUser;
import cn.com.haopy.yue.ai.wx.dto.WxCalculateInfoDto;
import cn.com.haopy.yue.ai.wx.dto.WxChargeCateDto;
import cn.com.haopy.yue.ai.wx.dto.WxChargeInfoDto;
import cn.com.haopy.yue.ai.wx.web.base.BaseWxController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * 登录
 */
@RestController
@RequestMapping("/haopyYueAi/standardCharges")
@RequiredArgsConstructor
@Slf4j
@Api(tags = "收费标准")
public class WxStandardChargesController extends BaseWxController {


    @Autowired
    private AIRiskStandardChargesService standardChargesService;

    @Autowired
    private AIRiskStandardChargesCollectService standardChargesCollectService;

    @Autowired
    private AIRiskStandardCalculateService standardCalculateService;

    @Autowired
    private AIRiskStandardCalculateItemService standardCalculateItemService;

    @Autowired
    private AIRiskStandardCategoryService standardCategoryService;

    @NonNull
    private AIRiskAdService adService;

    @GetMapping("/list")
    @ApiOperation(value = "标准列表")
    public Object link(@RequestParam(required = false) @ApiParam("搜索关键字") String keyWord) {
        return ResponseUtil.ok(standardChargesService.listAll(keyWord, null));
    }


    @GetMapping("/collectList")
    @ApiOperation(value = "我的收藏公式")
    public Object collectList(@LoginUser Integer userId) {
        List<AIRiskStandardCharges> list = new ArrayList<>();
        if (Objects.isNull(userId)) {
            list = standardChargesService.listAll(null, 9);
        } else {
            list = standardChargesService.myCollect(userId);
            if (Objects.isNull(list) || list.size() <= 0) {
                list = standardChargesService.listAll(null, 9);
            }
        }
        return ResponseUtil.ok(list);
    }

    @GetMapping("/collect")
    @ApiOperation(value = "计算公式收藏")
    public Object collect(@LoginUser Integer userId, @RequestParam @ApiParam("计算公式ID") Integer chargeId) {
        if (Objects.isNull(userId)) {
            return ResponseUtil.unlogin();
        }


        AIRiskStandardChargesCollect collect = standardChargesCollectService.getByUserChargeId(userId, chargeId);
        Boolean isCollect = false;
        if (Objects.isNull(collect)) {
            collect = new AIRiskStandardChargesCollect();
            collect.setChargeId(chargeId);
            collect.setUserId(userId);
            standardChargesCollectService.save(collect);
            isCollect = true;
        } else {
            standardChargesCollectService.removeById(collect);
        }

        Map<String, Object> data = new HashMap<>();
        data.put("isCollect", isCollect);

        return ResponseUtil.ok(data);

    }

    @GetMapping("/collectQuery")
    @ApiOperation(value = "计算公式收藏查询")
    public Object collectQuery(@LoginUser Integer userId, @RequestParam @ApiParam("计算公式ID") Integer chargeId) {
        if (Objects.isNull(userId)) {
            return ResponseUtil.unlogin();
        }
        AIRiskStandardChargesCollect collect = standardChargesCollectService.getByUserChargeId(userId, chargeId);
        Map<String, Object> data = new HashMap<>();
        Boolean isCollect = false;
        if (Objects.nonNull(collect)) {
            isCollect = true;
        }
        data.put("isCollect", isCollect);
        return ResponseUtil.ok(data);

    }

    @GetMapping("/chargeDetail")
    @ApiOperation(value = "标准详情")
    public Object chargeDetail(@RequestParam @ApiParam("标准ID") Integer chargeId) {
        AIRiskStandardCharges charge = standardChargesService.getById(chargeId);
        if (Objects.nonNull(charge)) {
            WxChargeInfoDto wxChargeInfoDto = new WxChargeInfoDto();
            BeanUtils.copyProperties(charge, wxChargeInfoDto);
            //计算公式
            if (wxChargeInfoDto.getIsSingle()) {

                //单收费计算
                AIRiskStandardCalculate calculate = standardCalculateService.getByChargeId(chargeId, null);
                WxCalculateInfoDto wxCalculateInfoDto = new WxCalculateInfoDto();
                BeanUtils.copyProperties(calculate, wxCalculateInfoDto);
                //计算明细
                wxCalculateInfoDto.setCalculateItem(standardCalculateItemService.getByCuId(calculate.getId()));
                //
                wxChargeInfoDto.setCalculateInfo(wxCalculateInfoDto);


            } else {
                //所有都取出来
                List<AIRiskStandardCategory> listCate = standardCategoryService.getCategory(chargeId, null);


                //所有计算公式
                List<AIRiskStandardCalculate> listCalculate = standardCalculateService.getListByChargeId(chargeId);
                //计算公式带明细
                List<WxCalculateInfoDto> listCul = new ArrayList<>();
                listCalculate.forEach(item -> {
                    WxCalculateInfoDto calculateInfoDto = new WxCalculateInfoDto();
                    BeanUtils.copyProperties(item, calculateInfoDto);
                    calculateInfoDto.setCalculateItem(standardCalculateItemService.getByCuId(item.getId()));
                    listCul.add(calculateInfoDto);
                });

                List<WxChargeCateDto> cateParent = new ArrayList<>();//父级
                List<WxChargeCateDto> cateLeaf = new ArrayList<>();//叶子节点
                //叶子节点
                for (AIRiskStandardCategory category : listCate) {
                    WxChargeCateDto wxChargeCateDto = new WxChargeCateDto();
                    BeanUtils.copyProperties(category, wxChargeCateDto);
                    if (category.getIsLeaf()) {
                        for (WxCalculateInfoDto calculateInfoDtoItem : listCul) {
                            if (calculateInfoDtoItem.getCateId().equals(category.getId())) {
                                wxChargeCateDto.setCalculateInfo(calculateInfoDtoItem);
                                break;
                            }
                        }
                    }


                    if (category.getPid().equals(0)) {
                        cateParent.add(wxChargeCateDto);
                    } else {
                        cateLeaf.add(wxChargeCateDto);
                    }
                }

                for (WxChargeCateDto cateParenItem : cateParent) {
                    if (cateParenItem.getIsLeaf()) {
                        continue;
                    }
                    List<WxChargeCateDto> children = new ArrayList<>();
                    for (WxChargeCateDto cateLeafItem : cateLeaf) {
                        if (cateParenItem.getId().equals(cateLeafItem.getPid())) {
                            children.add(cateLeafItem);
                        }
                    }
                    cateParenItem.setChildren(children);
                }

                wxChargeInfoDto.setTreeCate(cateParent);


            }


            if (Objects.nonNull(wxChargeInfoDto.getAdIds()) && wxChargeInfoDto.getAdIds().length > 0) {
                wxChargeInfoDto.setAdList(adService.listByIds(Arrays.asList(wxChargeInfoDto.getAdIds())));
            }

            return ResponseUtil.ok(wxChargeInfoDto);
        }
        return ResponseUtil.fail();
    }


}